<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         * 
         * 回文字串
         * 输入："abc"
         * 输出：3 
         * 解释：三个回文子串: "a", "b", "c"
         * 
         * 输入："aaa"
         * 输出：6
         * 解释：6个回文子串: "a", "a", "a", "aa", "aa", "aaa"
         * 
        */
        /* 
            时间：O(n ^ 3)
            空间：O(1)
        */
        // 双指针法--该方法会超时
        var countSubstrings = function(s) {
            let count = 0
            for (let i = 0; i < s.length; i++) {
                for (let j = i; j < s.length; j++) {
                    if (isHui(s.slice(i, j + 1))) {
                        count++
                    }
                }
            }
            return count
        };
        const isHui = function (s) {
            // i和j两个指针同时向两边移动
            for (let i = 0, j = s.length - 1; i < s.length, j >= i; i++, j--) {
                    if (s[i] !== s[j]) {
                        return false
                    }
            }
            return true
        }
        console.log(countSubstrings('abba'));
        // console.log(countSubstrings('aaa'));
    </script>
</body>

</html>